package com.puchi.sdkdemo.utils

import android.graphics.Point
import com.orhanobut.logger.Logger
import com.zalyyh.game.interfac.GameCallback
import com.zalyyh.mvvm.base.BaseActivity
import org.json.JSONObject
import com.bytedance.applog.AppLog
import com.google.gson.Gson
import com.puchi.sdkdemo.enty.H5enty
import org.json.JSONException

import android.content.Context.VIBRATOR_SERVICE
import android.os.*
import com.puchi.sdkdemo.JSBridge
import com.umeng.analytics.MobclickAgent

import layaair.game.browser.ExportJavaFunction
import android.R.attr.data
import com.zalyyh.mvvm.base.BaseViewModel




/*/**
* 创建一次性振动
*
* @param milliseconds 震动时长（ms）
* @param amplitude 振动强度。这必须是1到255之间的值，或者DEFAULT_AMPLITUDE
*/
VibrationEffect vibrationEffect = VibrationEffect.createOneShot(long milliseconds, int amplitude);

/**
* 创建波形振动
*
* @param timings 交替开关定时的模式，从关闭开始。0的定时值将导致定时/振幅对被忽略。
* @param repeat 振动重复的模式，如果您不想重复，则将索引放入计时数组中重复，或者-1。
*               -1 为不重复
*               0 为一直重复振动
*               1 则是指从数组中下标为1的地方开始重复振动，重复振动之后结束
*               2 从数组中下标为2的地方开始重复振动，重复振动之后结束
*/
VibrationEffect vibrationEffect = VibrationEffect.createWaveform(long[] timings, int repeat);

/**
* 创建波形振动
*
* @param timings 振幅值中的定时值。定时值为0振幅可忽视的。
* @param amplitudes 振幅值中的振幅值。振幅值必须为0和255之间，或为DEFAULT_AMPLITUDE。振幅值为0意味着断开。
* @param repeat 振动重复的模式，如果您不想重复，则将索引放入计时数组中重复，或者-1。
*/
VibrationEffect vibrationEffect = VibrationEffect.createWaveform(long[] timings, int[] amplitudes, int repeat);*/

class H5interactive private constructor() {
    var activity: BaseActivity<*, *>? = null
    var callback: GameCallback? = null

    companion object {
        var m_Handler = Handler(Looper.getMainLooper())
        private var instance: H5interactive? = null
            get() {
                if (field == null) {
                    field = H5interactive()
                }
                return field
            }

        @Synchronized
        fun get(): H5interactive {
            return instance!!
        }
    }

    /**
     * 0-----------> getAppConfigure
     * 1-----------> appLog_dot
     * 2-----------> loadBannerExpressAd
     * 3-----------> showInteractionExpressAd
     * 4-----------> showRewardVideoAd
     * 5-----------> loadNativeExpressAd
     * 6-----------> close_adver_dialog
     * 7-----------> setAppCallback
     * 8-----------> open_system_fun
     * 8-----------> open_system_fun
     * */
    fun interactive(key: Int, type: Int, value: String, keys: String) {
       // Logger.e("zalyyh interactive   key: "+key+"    type:  "+type+"    value:  "+value+"    keys:  "+keys)
        if (activity == null) return
        m_Handler.post {
            if (key == 0) {
                getSystemInfo(type)
            } else if (key == 1) {
                handle_key1(keys, value)
                if (callback!=null){
                    callback!!.interactive(key,type,value,keys)
                }
            } else if (key == 2) {
               if (callback!=null){
                   callback!!.interactive(key,type,value,keys)
               }
            } else if (key == 3) {
                if (callback!=null){
                    callback!!.interactive(key,type,value,keys)
                }
            } else if (key == 4) {
                if (callback!=null){
                    callback!!.interactive(key,type,value,keys)
                }
            } else if (key == 5) {
                if (callback!=null){
                    callback!!.interactive(key,type,value,keys)
                }
            } else if (key == 6) {
                if (callback!=null){
                    callback!!.interactive(key,type,value,keys)
                }
            } else if (key == 7) {
                if (callback!=null){
                    callback!!.interactive(key,type,value,keys)
                }
            } else if (key == 8) {
                open_system_fun(type)
            } else if (key == 9) {
                if (callback!=null){
                    callback!!.interactive(key,type,value,keys)
                }
            } else if (key == 10) {

            } else {
            }

        }
    }

    private fun handle_key1(keys: String, value: String) {
        var js = value
        if (js == null||js.length == 0||js.equals("null")){
            js = "{}"
        }

        try {
            var paramsObj = JSONObject(js);
            AppLog.onEventV3(keys, paramsObj);
            var it = paramsObj.keys();
            // 遍历jsonObject数据，添加到Map对象
            var map = hashMapOf<String,String>()
            while (it.hasNext()) {
                val key = it.next().toString()
                val value = paramsObj.get(key).toString() as String
                map.put(key,value)
            }
            MobclickAgent.onEvent(activity,keys, map);

        } catch (e: JSONException) {
            e.printStackTrace()
        }

    }

    private fun getSystemInfo(key: Int) {
        if (key == 0) {
            val outSize = Point()
            activity!!.getWindowManager().getDefaultDisplay().getRealSize(outSize)
            var a = H5enty()
            a.w = outSize.x.toFloat()
            a.h = outSize.y.toFloat()
            var json = Gson().toJson(a)
            ExportJavaFunction.CallBackToJS(JSBridge::class.java, "getSystemInfo", json)
        }
    }
    private fun open_system_fun(key: Int){
        if (key == 0) {
            val vibrator = activity!!.getSystemService(VIBRATOR_SERVICE) as Vibrator
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                vibrator.vibrate(VibrationEffect.createOneShot(1,-1))
            }else{
                vibrator.vibrate(10)
            }
        }
    }



}